package com.market.cash.device.util

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.text.TextUtils
import org.json.JSONObject
import java.net.Inet4Address
import java.net.NetworkInterface
import java.net.SocketException

object DVNetworkUtil {

    const val KEY_BSSID = "bssid"
    const val KEY_SSID = "ssid"
    const val KEY_MAC = "mac"

    fun getMacAddress(): String {
        var macAddress = ""
        try {
            val interfaces = NetworkInterface.getNetworkInterfaces()
            while (interfaces.hasMoreElements()) {
                val networkInterface = interfaces.nextElement()
                val hardwareAddress = networkInterface.hardwareAddress
                if (hardwareAddress != null && hardwareAddress.isNotEmpty()) {
                    val stringBuilder = StringBuilder()
                    for (b in hardwareAddress) {
                        stringBuilder.append(String.format("%02X:", b))
                    }
                    if (stringBuilder.isNotEmpty()) {
                        stringBuilder.deleteCharAt(stringBuilder.length - 1)
                    }
                    var currentMac = stringBuilder.toString()
                    if (!networkInterface.name.equals("wlan0", ignoreCase = true)) {
                        currentMac = macAddress
                    }
                    macAddress = currentMac
                }
            }
        } catch (e: SocketException) {
            e.printStackTrace()
        }
        return macAddress
    }

    fun getIPAddress(context: Context): String {
        try {
            val connectivityManager =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val info = connectivityManager.activeNetworkInfo
            if (info != null && info.isConnected) {
                if (info.type == ConnectivityManager.TYPE_MOBILE) {
                    val interfaces = NetworkInterface.getNetworkInterfaces()
                    while (interfaces.hasMoreElements()) {
                        val networkInterface = interfaces.nextElement()
                        val enumIpAddr = networkInterface.inetAddresses
                        while (enumIpAddr.hasMoreElements()) {
                            val inetAddress = enumIpAddr.nextElement()
                            if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                                return inetAddress.hostAddress ?: ""
                            }
                        }
                    }
                } else if (info.type == ConnectivityManager.TYPE_WIFI) {
                    val wifiManager =
                        context.getSystemService(Context.WIFI_SERVICE) as WifiManager
                    val wifiInfo: WifiInfo? = wifiManager.connectionInfo
                    wifiInfo?.let {
                        return convertToIPString(wifiInfo.ipAddress)
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }

    private fun convertToIPString(ip: Int): String {
        return "${ip and 0xFF}.${ip shr 8 and 0xFF}.${ip shr 16 and 0xFF}.${ip shr 24 and 0xFF}"
    }

    fun getCurrentWifi(context: Context): String {
        var curWifi = ""
        try {
            val jsonObject = JSONObject()
            val wifiManager =
                context.getSystemService(Context.WIFI_SERVICE) as WifiManager
            val wifiInfo: WifiInfo? = wifiManager.connectionInfo
            wifiInfo?.let {
                val bssid = wifiInfo.bssid.safe()
                val ssid = (wifiInfo.ssid.replace("\"", "")).safe()
                val mac = getMacAddress().safe()
                val safeBssid = if (TextUtils.isEmpty(bssid)) "" else bssid
                jsonObject.put(KEY_BSSID, safeBssid)
                jsonObject.put(KEY_SSID, ssid)
                jsonObject.put(KEY_MAC, mac)
            }
            curWifi = jsonObject.toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return curWifi
    }

    fun getVpnState(context: Context): String {
        return try {
            val connectivityManager =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo
            if (activeNetwork != null && activeNetwork.type == ConnectivityManager.TYPE_VPN) {
                "YES"
            } else {
                "NO"
            }
        } catch (e: Exception) {
            e.printStackTrace()
            "NO"
        }
    }

    fun getIsWifiProxy(): String {
        return try {
            val proxyHost = System.getProperty("http.proxyHost")
            val proxyPort = System.getProperty("http.proxyPort")
            if (proxyHost != null && proxyPort != null) {
                "YES"
            } else {
                "NO"
            }
        } catch (e: Exception) {
            e.printStackTrace()
            "NO"
        }
    }
}
